package com.mijie.indexall;

//indexnbs.AutoIndex_ContextListener

import java.util.Timer;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.chenlb.mmseg4j.example.Complex;
import com.mijie.homi.search.api.UserSearchApi;
import com.mijie.homi.search.service.index.ClrRecently;
import com.mijie.homi.search.service.index.MoodUserIndexService;
import com.mijie.homi.search.service.index.clr;
import com.mijie.homi.search.util.god;

/**
 * @date 2010-1-14
 * @author wangpeng 功能：监听器，用来实现计时器
 */
@Component
public class AutoIndex_ContextListener extends HttpServlet implements
		ServletContextListener {
	/**
  * 
  */
	private static final long serialVersionUID = 1L;
	private static final Logger log = indexControllor4auto.logger;

	//@Autowired
//	@Resource(name="clr")
	private clr clr_thread;

	private Timer timer = null;
	public static  WebApplicationContext ctx;
	ServletContextEvent thisevent;

	public void contextInitialized(ServletContextEvent event) {
	//	if(1==1)return;

		thisevent = event;
		log.info("autoindex定时器已启动");
		// if(1==1)
		// {
		// log.info(" exit auto inex");
		// return;
		// }
		god.newThread(new Runnable() {

			@Override
			public void run() {
				indexControllor4auto.startAutoIndex();

			}
		}, "auto_index_thread ");

		
		//o19  ClrRecently_thread
//		god.newThread(new Runnable() {
//
//			@Override
//			public void run() {
//				try{
//				ClrRecently c = (ClrRecently) getBean("ClrRecently");
//				// List<Map> li = .selectList("getRecently100", nowId);
//				System.out.println(c);
//				c.start();
//				}catch (Exception e) {
//					MoodUserIndexService.logger.error(god.getTrace(e));
//					MoodUserIndexService.loggerT.error(god.getTrace(e));
//				}
//
//			}
//		}, "ClrRecently_thread ");
//
//
//	
//		
//		
//		god.newThread(new Runnable() {
//
//			@Override
//			public void run() {
//				try {
//					clr c = clr_thread;
//					c.start();
//					String string = "----clr_index_thread root ok ";
//					MoodUserIndexService.logger.info(string);
//					MoodUserIndexService.loggerT.info(string);
//				} catch (Exception e) {
//					MoodUserIndexService.logger.error(god.getTrace(e));
//					MoodUserIndexService.loggerT.error(god.getTrace(e));
//
//					try {
//						String string = "----clr_index_thread root   by  getBean";
//						MoodUserIndexService.logger.info(string);
//						MoodUserIndexService.loggerT.info(string);
//						clr c = (clr) getBean("clr");
//						c.start();
//
//					
//					} catch (Exception ex) {
//						MoodUserIndexService.logger.error(god.getTrace(ex));
//						MoodUserIndexService.loggerT.error(god.getTrace(ex));
//					}
//					// ApplicationContext context =
//					// WebApplicationContextUtils.getWebApplicationContext(application);
//				}
//
//			}
//		}, "clr_index_thread ");

		log.info(" exit  contextInitialized ");
	}

	public void contextDestroyed(ServletContextEvent event) {

		log.info("autoindex定时器销毁");
	}

	/**
	 * 根据名称，可以获得ApplicationContext中配置的 bean
	 * 
	 * @param name
	 * @return
	 */
	public Object getBean(String name) {
		if (ctx == null) {
			ctx = WebApplicationContextUtils
					.getRequiredWebApplicationContext(thisevent
							.getServletContext());
		}
		return ctx.getBean(name);
	}

	/**
	 * for only serlevet
	 */
	public void init() throws ServletException {
		contextInitialized(null);
		// 想要写的东西

		// TimerThead tt=new TimerThead();

		// tt.TimerStart();

	}
}